﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace HelperLib
{
   public class XMLReader
   {
      private readonly XmlSerializer serializer;
      private FileStream filestream;

      public XMLReader()
      {
         serializer = new XmlSerializer(typeof(List<KukaCoordData>));
      }

      public bool readData(string filepath, ref List<KukaCoordData> coords, out string output)
      {
         try
         {
            filestream = new FileStream(@filepath, FileMode.OpenOrCreate);
            coords = (List<KukaCoordData>)serializer.Deserialize(filestream);
            filestream.Close();

            if (coords.Count == 0)
               output = "- Warning - XML-Data read but no data in xml-file.";
            else
               output = "XML-Data read.";
 
            return true;
         }
         catch (Exception ex)
         {
            filestream.Close();
            output = "- Error - " + ex.Message;
            MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
         return false;
      }
   }
}
